package com.zyh.flower;

public class FlowerServiceImpl implements FlowerService {
    @Override
    public void add() {
        System.out.println("请输入您要新添加的订单编号：");
        String addId = FlowerService.INPUT.next();
        for (int i = 0; i < FlowerService.orderList.length; i++) {
            if (FlowerService.orderList[i].getId().equals(addId)) {//说明订单编号已经存在
                System.out.println("您输入的订单编号已经存在，请重新输入：");
                return;
            }
        }
        //走到此处说明输入的编号为新的编号
        String newName = null;
        int newNumber = 0;
        double newPrice = 0;
        String newDate = null;
        String newSalEmp = null;
        String newBeiZhu = null;
        System.out.println("请输入新订单的鲜花名称：");
        newName = FlowerService.INPUT.next();
        //完善功能
        while (true) {
            System.out.println("请输入新订单的销售数量：");
            newNumber = FlowerService.INPUT.nextInt();
            if (newNumber < 0) {
                System.err.println("输入有误！");
                continue;
            } else {
                break;
            }
        }
        while (true) {
            System.out.println("请输入新订单的鲜花价格：");
            newPrice = FlowerService.INPUT.nextDouble();
            if (newPrice < 0) {
                System.err.println("输入有误！");
                continue;
            } else {
                break;
            }
        }
        System.out.println("请输入新订单的销售日期：");
        newDate = FlowerService.INPUT.next();
        System.out.println("请输入新订单的销售员：");
        newSalEmp = FlowerService.INPUT.next();
        System.out.println("请输入新订单的备注：");
        newBeiZhu = FlowerService.INPUT.next();
        FlowerOrder[] newOrder = new FlowerOrder[6];
        FlowerOrder order = new FlowerOrder(addId, newName, newNumber, newPrice, newDate, newSalEmp, newBeiZhu);
        System.out.println("您确定要添加吗？（y/n）");
        String confirm = FlowerService.INPUT.next();
        if ("y".equals(confirm)) {
            //创建长度为6的数组
            for (int i = 0; i < newOrder.length - 1; i++) {
                newOrder[i] = FlowerService.orderList[i];
                newOrder[5] = order;
            }
        }
        printHead();
        for (int i = 0; i < newOrder.length; i++) {
            System.out.println(newOrder[i].getId() + "\t" + newOrder[i].getName() + "\t" + newOrder[i].getNumber() + "\t\t" + newOrder[i].getPrice() + "\t" + newOrder[i].getDate() + "\t" + newOrder[i].getSalEpy() + "\t" + newOrder[i].getBeiZhu());
        }
    }

    @Override
    public void delete() {
        System.out.println("请输入要删除的订单编号：");
        String deleteId = FlowerService.INPUT.next();
        //假设输入错误的编号
        boolean flag = false;
        for (int i = 0; i < FlowerService.orderList.length; i++) {
            //根据输入的编号来查询是否存在该编号
            if (FlowerService.orderList[i].getId().equals(deleteId)) {
                //存在则改为true并结束遍历数组
                flag = true;
                break;
            }
        }
        //循环走完后flag的值没修改，说明数组中不存在输入的编号
        if (flag == false) {
            System.err.println("对不起，没有您要删除的销售编号！");
            return;
        }
        System.out.println();
        //走到此处说明没有执行上面if里面的break，说明输入的编号是对的
        System.out.println("您要删除的订单详细信息如下：");
        printHead();
        for (int i = 0; i < FlowerService.orderList.length; i++) {
            if (FlowerService.orderList[i].getId().equals(deleteId)) {
                System.out.println(FlowerService.orderList[i].getId() + "\t" + FlowerService.orderList[i].getName() + "\t" + FlowerService.orderList[i].getNumber() + "\t\t" + FlowerService.orderList[i].getPrice() + "\t" + FlowerService.orderList[i].getDate() + "\t" + FlowerService.orderList[i].getSalEpy() + "\t" + FlowerService.orderList[i].getBeiZhu());
            }
        }
        System.out.println("确认要删除吗？（y/n）");
        String confirm = FlowerService.INPUT.next();
        if ("y".equals(confirm)) {
            //删除这条数据
            //创建新数组
            FlowerOrder[] newOrder = new FlowerOrder[4];
            int index = 0;
            for (int i = 0; i < FlowerService.orderList.length; i++) {
                if (FlowerService.orderList[i].getId().equals(deleteId)) {
                    continue;
                } else {
                    newOrder[index++] = FlowerService.orderList[i];
                }
                if (index == newOrder.length) {
                    break;
                }
            }
            System.out.println("删除成功！");
            printHead();
            for (int i = 0; i < newOrder.length; i++) {
                System.out.println(newOrder[i].getId() + "\t" + newOrder[i].getName() + "\t" + newOrder[i].getNumber() + "\t\t" + newOrder[i].getPrice() + "\t" + newOrder[i].getDate() + "\t" + newOrder[i].getSalEpy() + "\t" + newOrder[i].getBeiZhu());
            }
        }
    }

    @Override
    public void update() {
        System.out.println("请输入销售编号：");
        String uid = FlowerService.INPUT.next();
        //判断用户输入的uid是否能在数组中找到
        boolean flag = false;
        for (int i = 0; i < FlowerService.orderList.length; i++) {
            if (FlowerService.orderList[i].getId().equals(uid)) {
                flag = true;
                break;
            }
        }
        if (flag == false) {
            System.err.println("对不起，您要修改的订单号不存在！");
            return;
        }
        System.out.println("您要修改的订单详细信息如下：");
        System.out.println();
        //打印表头
        printHead();
        for (int i = 0; i < orderList.length; i++) {
            if (orderList[i].getId().equals(uid)) {
                System.out.println(orderList[i].getId() + "\t" + orderList[i].getName() + "\t" + orderList[i].getNumber() + "\t\t" + orderList[i].getPrice() + "\t" + orderList[i].getDate() + "\t" + orderList[i].getSalEpy() + "\t" + orderList[i].getBeiZhu());
            }
        }
        System.out.println();
        String newName = null;
        int newNumber = 0;
        double newPrice = 0;
        String newDate = null;
        String newSalEmp = null;
        String newBeiZhu = null;
        System.out.println("请输入修改后的鲜花名称：");
        newName = INPUT.next();
        //完善功能
        while (true) {
            System.out.println("请输入修改后的销售数量：");
            newNumber = INPUT.nextInt();
            if (newNumber < 0) {
                System.err.println("输入有误！");
                continue;
            } else {
                break;
            }
        }
        while (true) {
            System.out.println("请输入修改后的鲜花价格：");
            newPrice = INPUT.nextDouble();
            if (newPrice < 0) {
                System.err.println("输入有误！");
                continue;
            } else {
                break;
            }
        }
        System.out.println("请输入修改后的销售日期：");
        newDate = INPUT.next();
        System.out.println("请输入修改后的销售员：");
        newSalEmp = INPUT.next();
        System.out.println("请输入修改后的备注：");
        newBeiZhu = INPUT.next();

        System.out.println("您确定要修改吗？（y/n）");
        String confirm = INPUT.next();
        if ("y".equals(confirm)) {
            //创建一个新对象存入修改后的数据
            FlowerOrder order = new FlowerOrder(uid, newName, newNumber, newPrice, newDate, newSalEmp, newBeiZhu);
            //将新对象替换为原来uid的对象
            for (int i = 0; i < orderList.length; i++) {
                if (orderList[i].getId().equals(uid)) {
                    orderList[i] = order;
                }
            }
            System.out.println("修改成功！");
            printHead();
            //输出一遍
            printArray(orderList);
        }
    }

    @Override
    public void findAll() {
//1.打印表头
        printHead();
        //2.遍历数组输出
        printArray(orderList);
    }

    @Override
    public void printHead() {
        System.out.println("编号\t\t鲜花名称\t\t\t销售数量\t价格\t\t销售日期\t\t销售员\t备注");
    }

    @Override
    public void printArray(FlowerOrder[] orderLister) {
        for (int i = 0; i < orderList.length; i++) {
            System.out.println(orderList[i].getId() + "\t" + orderList[i].getName() + "\t" + orderList[i].getNumber() + "\t\t" + orderList[i].getPrice() + "\t" + orderList[i].getDate() + "\t" + orderList[i].getSalEpy() + "\t" + orderList[i].getBeiZhu());
        }
    }
}
